<?php

namespace app\api\controller\mox;

use app\common\controller\Api;
use app\admin\model\mox\inherit\Character as CharacterModel;

/**
 * 人物
 */
class Character extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 人物列表
     *
     */
    public function list()
    {
        $model = new CharacterModel();
        $list = $model->order('id desc')
            ->paginate();
        $this->success('请求成功', $list);
    }
    /**
     * 人物详情
     */
    public function characterDetail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('缺少参数id');
        }
        $model = new CharacterModel();
        $info = $model->where('id', $id)->find();
        if (!$info) {
            $this->error('人物不存在');
        }
        $info['user'] = model('app\common\model\User')->where('id', $info['user_id'])->field('id, nickname, avatar')->find();
        $related_persons = explode(',', $info['related_persons']);
        $info['related_persons'] = model('app\admin\model\mox\inherit\Character')->where('id', 'in', $related_persons)->field('id, name,image')->select();
        $related_plays = explode(',', $info['related_plays']);
        $info['related_plays'] = model('app\admin\model\mox\play\Info')->where('id', 'in', $related_plays)->field('id, title, image,type_id,brief_introduction,create_time,update_time')->select();
        foreach ($info['related_plays'] as $k => $v) {
            $v->type = model('app\admin\model\mox\play\Type')->where('id', $v['type_id'])->field('id, title,image')->find();
        }
        $info['plays_num'] = count($related_plays);
        $info['persons_num'] = count($related_persons);
        $this->success('请求成功', $info);
    }
}
